package pyrachnid.model.ground_model;

import java.util.Random;

import pyrachnid.model.ObservableWithEvent;
import pyrachnid.model.OperationWind;

public class Wind extends ObservableWithEvent<OperationWind> {
	private Direction _direction;
	private int _force;
	private int _nextUpdate;
	private int _oneTop;
	
	public enum Direction
	{
		North,
		South,
		East,
		West,
		North_East,
		South_East,
		North_West,
		South_West,
	}
	
	public Wind(Direction p_direction, int p_force) {
		if(p_force < 0)
		{
			this._force = 0;
		}
		else if(p_force > 100)
		{
			this._force = 100;
		}
		else
		{
			this._force = p_force;
		}
		this._direction = p_direction;

		// Un top toutes les secondes
		_oneTop = 250;
		_nextUpdate = _oneTop;
	}
	
	public Direction getDirection() {
		return _direction;
	}

	public int getForce() {
		return _force;
	}
	
	private void computeForce()
	{
		int windValueRand = new Random().nextInt(5);
		
		if((windValueRand == 0 && _force > 0) || (windValueRand == 1 && _force < 100))
		{
			_force += (windValueRand == 0)? -1 : 1;
			notifyObserver(this, "Value Change");
		}
	}
	
	private void computeDirection()
	{
		int windValueRand = new Random().nextInt(100);
		
		if(windValueRand < 8)
		{
			//Initialisation
			Direction newDirection = Direction.North;
			
			// Choose Direction
			if(windValueRand == 0) newDirection = Direction.North;
			else if(windValueRand == 1) newDirection = Direction.South;
			else if(windValueRand == 2) newDirection = Direction.East;
			else if(windValueRand == 3) newDirection = Direction.West;
			else if(windValueRand == 4) newDirection = Direction.North_East;
			else if(windValueRand == 5) newDirection = Direction.North_West;
			else if(windValueRand == 6) newDirection = Direction.South_East;
			else if(windValueRand == 7) newDirection = Direction.South_West;
			
			// If new Direction, change it
			if(!newDirection.equals(_direction))
			{
				_direction = newDirection;
				notifyObserver(this, "Direction Change");
			}
		}
	}
	
	public void update(int p_time) {
		_nextUpdate -= p_time;
		
		if(_nextUpdate <= 0)
		{	
			computeForce();
			computeDirection();
			
			_nextUpdate += _oneTop;
		}
	}
}